package com.commander.spring.cloud.config;

import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author zhangxin
 */
@Configuration
public class GatewayConfiguration {
	
	private final List<ViewResolver> viewResolvers;
	private final ServerCodecConfigurer serverCodecConfigurer;
	
	public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
	                            ServerCodecConfigurer serverCodecConfigurer) {
		this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
		this.serverCodecConfigurer = serverCodecConfigurer;
	}
	
//	@Bean
//	@Order(Ordered.HIGHEST_PRECEDENCE)
//	public GlobalFilter sentinelGatewayFilter() {
//		return new SentinelGatewayFilter();
//	}
	
	@Bean
	@Order(Ordered.HIGHEST_PRECEDENCE)
	public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
		return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
	}

//	@PostConstruct
//	public void doInit() {
//		initCustomizedApis();
//		initGatewayRules();
//	}
//
//	private void initCustomizedApis() {
//		Set<ApiDefinition> definitions = new HashSet<>();
//		ApiDefinition api1 = new ApiDefinition("demo-consumer")
//						                     .setPredicateItems(new HashSet<ApiPredicateItem>() {{
//							                     add(new ApiPathPredicateItem().setPattern("/demo-consumer/**")
//											                         .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
//						                     }});
//
//		definitions.add(api1);
//		GatewayApiDefinitionManager.loadApiDefinitions(definitions);
//	}
//
//	/**
//	 * 配置限流规则
//	 */
//	private void initGatewayRules() {
//		Set<GatewayFlowRule> rules = new HashSet<>();
//		rules.add(new GatewayFlowRule("demo-consumer")
//						          .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
//						          // 限流阈值
//						          .setCount(1)
//						          // 统计时间窗口，单位是秒，默认是 1 秒
//						          .setIntervalSec(1)
//		);
//
//		rules.add(new GatewayFlowRule("baidu")
//						          .setCount(1)
//						          .setIntervalSec(1)
//		);
//
//		GatewayRuleManager.loadRules(rules);
//	}
}